package com.yhsoft.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhsoft.common.exception.BusinessException;
import com.yhsoft.common.service.TokenService;
import com.yhsoft.sys.controller.Response.CustomerOutVo;
import com.yhsoft.sys.controller.request.CustomerAddInVo;
import com.yhsoft.sys.controller.request.CustomerSelectInVo;
import com.yhsoft.sys.controller.request.CustomerUpdateInVo;
import com.yhsoft.sys.entity.CustomerInfo;
import com.yhsoft.sys.mapper.CustomerInfoMapper;
import com.yhsoft.sys.service.ICustomerInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 客户企业管理 服务实现类
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-10-15
 */
@Service
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements ICustomerInfoService {
    @Resource
    private CustomerInfoMapper customerInfoMapper;

    @Override
    public String addCustomerInfo(CustomerAddInVo inVo) {
        // 企业名称不能重复
        if(StringUtils.isEmpty(inVo.getCustomerName())){
            throw new BusinessException("企业名称不能为空");
        }

        LambdaQueryWrapper<CustomerInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CustomerInfo::getCustomerName, inVo.getCustomerName());
        List<CustomerInfo> list = customerInfoMapper.selectList(queryWrapper);
        if (list.size() > 0){
            throw new RuntimeException("企业名称已存在");
        }

        CustomerInfo customerInfo = new CustomerInfo();
        customerInfo.setCompanyId(TokenService.getUserBytoken().getCompanyId());
        BeanUtils.copyProperties(inVo, customerInfo);
        customerInfoMapper.insert(customerInfo);

        // 查询主键
        CustomerInfo customerInfo1 = customerInfoMapper.selectOne(queryWrapper);
        return customerInfo1.getId().toString();
    }

    @Override
    public void updateCustomerInfo(CustomerUpdateInVo inVo) {
        CustomerInfo customerInfo = new CustomerInfo();
        BeanUtils.copyProperties(inVo, customerInfo);
        customerInfoMapper.updateById(customerInfo);
    }

    @Override
    public void delCustomer( List<Integer> ids) {
        LambdaQueryWrapper<CustomerInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(CustomerInfo::getId, ids);
        customerInfoMapper.delete(wrapper);
    }

    @Override
    public IPage<CustomerOutVo> getCustomerList(CustomerSelectInVo inVo) {
        LambdaQueryWrapper<CustomerInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(inVo.getCustomerName()),CustomerInfo::getCustomerName, inVo.getCustomerName());
        queryWrapper.like(StringUtils.isNotEmpty(inVo.getLinkMan()),CustomerInfo::getLinkMan, inVo.getLinkMan());
        queryWrapper.like(StringUtils.isNotEmpty(inVo.getLinkPhone()),CustomerInfo::getLinkPhone, inVo.getLinkPhone());
        queryWrapper.eq(CustomerInfo::getCompanyId, TokenService.getUserBytoken().getCompanyId());

        IPage<CustomerInfo> page = new Page<>(inVo.getPageNo(), inVo.getPageSize());
        IPage<CustomerInfo> customerInfoIPage = customerInfoMapper.selectPage(page, queryWrapper);

        IPage<CustomerOutVo> convert = customerInfoIPage.convert(customerInfo -> {
            CustomerOutVo form = new CustomerOutVo();
            BeanUtils.copyProperties(customerInfo, form);
            return form;
        });

        return convert;
    }
}
